import { toSuccess, useEntityManager } from "@sker/core"
import { KsAccount, KsLiveRoomLog } from "@sker/entities"
import { useData } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({})
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const list = await m.createQueryBuilder()
            .select('count(1)', 'count')
            .addSelect('sum(log.count)', 'total')
            .addSelect('max(log.count)', 'max_count')
            .addSelect('log.user_id', 'user_id')
            .addSelect('account.name', 'name')
            .from(KsLiveRoomLog, 'log')
            .leftJoin(KsAccount, 'account', 'account.user_id=log.user_id')
            .groupBy('log.user_id')
            .addGroupBy('account.name')
            .orderBy('count', 'DESC')
            .limit(1000)
            .execute()
        return toSuccess(list)
    })
}